package com.cdh.edu.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdh.edu.entity.Teacher;
import com.cdh.edu.entity.vo.TeacherQuery;
import com.cdh.edu.feign.OssFileService;
import com.cdh.edu.service.TeacherService;
import com.cdh.utils.result.Restful;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cdh
 * @since 2020-10-19
 */
@ApiModel(description = "讲师管理")
@RestController
@RequestMapping("teacher")
@CrossOrigin
@Slf4j
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @Autowired
    private OssFileService ossFileService;

    /**
     * 获取所有讲师
     * @return
     */
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public Restful finaAllTeacher(){
        //调用service的方法实现查询所有的操作
        return Restful.ok().data("items",teacherService.list(null));
    }

    /**
     * 2.逻辑删除讲师的方法
     */
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}") //id需要通过路径进行传递
    public Restful removeTeacher(@ApiParam(name = "id",value = "讲师ID",required = true) @PathVariable String id){
        // @PathVariable String id 获取路径中的id值

        //删除讲师头像
        ossFileService.removeFileAvatar(id);

        boolean flag = teacherService.removeById(id);
        if (flag){
            return Restful.ok();
        }else {
            return Restful.error();
        }
    }
    /**
     3.分页查询讲师  current 当前页 limit 每页记录数
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("pageTeacher/{current}/{limit}")
    public Restful pageListTeacher(@PathVariable long current,
                                   @PathVariable long limit){
        //创建page对象
        Page<Teacher> pageT = new Page<>(current,limit);
        //调用方法实现分页
        teacherService.page(pageT,null);

        //总记录数
        long total = pageT.getTotal();
        List<Teacher> records = pageT.getRecords();

        return Restful.ok().data("total",total).data("rows",records);
    }

    /**
     *  4.条件查询带分页的方法
    */
    @ApiOperation(value = "带条件的分页查询")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public Restful pageTeacherCondition(@PathVariable long current
            , @PathVariable long limit
            ,@RequestBody(required = false) TeacherQuery tq){
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        //创建page对象
        Page<Teacher> pageT = new Page<>(current,limit);


        // try{
        //     int i = 1/0;
        // }catch (Exception e){
        //     //手动执行异常处理
        //     throw new DIYException(201,"执行了自定义异常处理！");
        // }

        //构建条件
        //多条件组合查询
        String name = tq.getName();
        Integer level = tq.getLevel();
        String begin = tq.getBegin();
        String end = tq.getEnd();
        //判断条件之是否为空，如果不为空则拼接条件
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }

        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }

        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }

        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");
        // 调用方法实现分页
        teacherService.page(pageT,wrapper);

        long total = pageT.getTotal();
        List<Teacher> records = pageT.getRecords();
        return Restful.ok().data("total",total).data("rows",records);
    }

    /**
     * 添加讲师接口的方法
     */
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public Restful addTeacher(@RequestBody Teacher t){
        if (teacherService.save(t)){
            return Restful.ok();
        }else{
            return Restful.error();
        }
    }

    /**
     * 根据工程师ID进行查询
     */
    @ApiOperation(value = "根据讲师ID进行查询")
    @GetMapping("getTeacher/{id}")
    public Restful getTeacher(@PathVariable String id){
        Teacher t = teacherService.getById(id);
        return Restful.ok().data("teacher",t);
    }


    /**
     * 更新讲师
     */
    @ApiOperation(value = "更新讲师")
    @PostMapping("updateTeacher")
    public Restful updateTeacher(@RequestBody Teacher t){
        boolean flag = teacherService.updateById(t);
        if (flag){
            return Restful.ok();
        }else{
            return Restful.error();
        }
    }



}

